<?php
    class Admin_Model_Orders extends Zend_Db_Table_Abstract
    {
        protected $_name = 'orders';
        protected $_primary = 'orders_id';
        protected $_db;
        
        public function __construct() {
            $this->_db = Zend_Registry::get('db');
        }
        public function listTour(){
            $sql = "select a.tour_name,
                           date_format(b.time,'%m/%d/%Y') as time,
                           b.status,
                           c.fullname,
                           b.orders_id
                    from tour as a 
                    inner join orders as b
                    inner join user as c
                    on a.tour_id=b.tour_id
                    and b.user_id = c.user_id
                    order by b.orders_id desc";
            $result = $this->_db->fetchAll($sql);
            return $result;
        }
        public function deleteTours($id){
            $db = Zend_Db_Table::getDefaultAdapter();
             foreach ($id as $value){
                $where = $db->quoteInto('orders_id = ?', $value);
                $this->_db->delete($this->_name, $where);
                
                }
        }
        public function deleteTour($id){
            $db = Zend_Db_Table::getDefaultAdapter();
            $where = $db->quoteInto('orders_id = ?', $id);
            $db->delete($this->_name, $where);
        }
        
        public function getOrdersById($orders_id) {
            $user = array(
                'u.fullname',
                'u.email',
            );
            $tour = array(
                't.tour_name',
            );
            $orders = array(
                'o.time',
                'o.status',
            );
            $order_detail = array(
                'od.adult_qty',
                'od.children_qty',
                'od.total_cost',
            );
            $sql = $this->_db->select()
                            ->from('orders AS o', $orders)
                            ->join('user AS u', 'u.user_id = o.user_id', $user)
                            ->join('tour AS t', 't.tour_id = o.tour_id', $tour)
                            ->join('order_detail AS od', 'od.order_id = o.orders_id', $order_detail)
                            ->where('od.order_id = ?', $orders_id);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function finishOrders($orders_id, $data) {
            $where = $this->getAdapter()->quoteInto('orders_id = ?', $orders_id);
            $this->_db->update($this->_name, $data, $where);
        }
    }
